package com.microstation.service.impl;
import java.util.List;

import com.microstation.enums.ResultEnum;
import com.microstation.exception.ElementEmptyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.microstation.service.AdminInfoService;
import com.microstation.dao.AdminInfoDao;
import com.microstation.common.Assist;
import com.microstation.common.Result;
import com.microstation.entity.po.AdminInfo;
import org.springframework.transaction.annotation.Transactional;

/**
 * AdminInfo的服务接口的实现类
 * 
 * @author 
 *
 */
@Service
public class AdminInfoServiceImpl implements AdminInfoService {
	private final Logger LOG = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private AdminInfoDao adminInfoDao;

	@Override
	public Result find(Assist assist) {
		//TODO这里可以做通过Assist做添加查询
		long results = adminInfoDao.getAdminInfoRowCount(assist);
		List<AdminInfo> result = adminInfoDao.selectAdminInfo(assist);
		return new Result(results, result);
	}
	@Override
	public AdminInfo findOne(Long id) {
		if (id == null) {
			return null;
		}
		AdminInfo result = adminInfoDao.selectAdminInfoById(id);
		return result;
	}

	@Override
	public Integer saveNotNull(AdminInfo value) {
		if (value == null) {
			return null;
		}
		if(value.getRoleId() == null  ){
        	throw new ElementEmptyException(ResultEnum.EMPTY.getInfo());
		}
		int result = adminInfoDao.insertNotNullAdminInfo(value);
		return result;
	}
	@Override
	public Integer updateNotNullById(AdminInfo value) {
		if (value == null) {
			return null;
		}
		int result = adminInfoDao.updateNotNullAdminInfoById(value);
		return result;
	}

	@Override
	public Integer deleteById(Long id) {
		if (id == null) {
			return null;
		}
		int result = adminInfoDao.deleteAdminInfoById(id);
		return result;
	}

	@Override
	@Transactional
	public Result saveAdmin(AdminInfo adminInfo) {
		try {
			int result = adminInfoDao.insertNotNullAdminInfo(adminInfo);
			if (result != 0) {
				return new Result(false, ResultEnum.SUCCESS.getType(), ResultEnum.SUCCESS.getInfo(), null);
			}
			return new Result(true, ResultEnum.FAIL.getType(), "已存在相同账号", null);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	@Transactional
	public Result updateAdminInfo(AdminInfo adminInfo) {
		try {
			int result = adminInfoDao.updateNotNullAdminInfoById(adminInfo);
			if (result != 0) {
				return new Result(false, ResultEnum.SUCCESS.getType(), ResultEnum.SUCCESS.getInfo(), null);
			}
			return new Result(true, ResultEnum.FAIL.getType(), "已存在相同账号", null);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	@Transactional
	public Result removeAdmin(String ids) {
		String[] idarr = ids.split(",");
		try {
			for (String id : idarr) {
				int i = adminInfoDao.deleteAdminInfoById(Long.parseLong(id));
			}
			return new Result(false, ResultEnum.SUCCESS.getType(), null);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public AdminInfo getByExample(AdminInfo adminInfo) {
		return adminInfoDao.selectAdminInfoObjSingle(adminInfo);
	}
}